<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\response\Json;
use think\Validate;

class CarouselController extends BaseController
{
    public function list(Request $request): Json
    {
        try {
            $param = $request->param();
            $page  = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;
            $map   = [];
            if ($param['title'] ?? "") $map[] = ['title', 'like', '%' . $param['title'] . '%'];
            if ($param['type'] ?? "") $map[] = ['type', '=', $param['type']];

            $res = Db::name('carousel')
                ->where($map)
                ->order('sort', 'asc')
                ->paginate($limit)
                ->toArray();

            $cos = config('basic.cdn');
            foreach ($res['data'] as $key => $value) {
                $res['data'][$key]['file_url'] = $cos . $res['data'][$key]['file_url'];
                $res['data'][$key]['jump_url'] = $cos . $res['data'][$key]['jump_url'];
            }

            return json(['code' => 0, 'msg' => 'success', 'data' => $res]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => 'error：' . $e->getMessage()]);
        }
    }

    public function create(Request $request): Json
    {
        try {
            $rules = [
                'id'           => 'require',
                'carousel_id'  => 'require',
                'title'        => 'require',
                'tags'         => 'require',
                'cover_image'  => 'require',
                'type'         => 'require',
                'file_url'     => 'require',
                'click_action' => 'require',
                'jump_url'     => 'require',
                'sort'         => 'require',
                'is_display'   => 'require',
            ];

            $messages = [
                'id.require'           => 'id必须',
                'carousel_id.require'  => 'carousel_id必须',
                'title.require'        => 'title必须',
                'tags.require'         => 'tags必须',
                'cover_image.require'  => 'cover_image必须',
                'type.require'         => 'type必须',
                'file_url.require'     => 'file_url必须',
                'click_action.require' => 'click_action必须',
                'jump_url.require'     => 'jump_url必须',
                'sort.require'         => 'sort必须',
                'is_display.require'   => 'is_display必须',
            ];

            $all                = $request->all();
            $cos                = config('basic.cdn');
            $all['file_url']    = str_replace($cos, '', $all['file_url']);
            $all['jump_url']    = str_replace($cos, '', $all['jump_url']);
            $all['cover_image'] = str_replace($cos, '', $all['cover_image']);
            Validate($rules)->message($messages)->check($all);

            Db::name('carousel')->create($all);

            return json(['code' => 0, 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => '失败：' . $e->getMessage()]);
        }

    }

    public function update(Request $request): Json
    {
        try {
            $rules = [
                'id'           => 'require',
                'carousel_id'  => 'require',
                'title'        => 'require',
                'tags'         => 'require',
                'cover_image'  => 'require',
                'type'         => 'require',
                'file_url'     => 'require',
                'click_action' => 'require',
                'jump_url'     => 'require',
                'sort'         => 'require',
                'is_display'   => 'require',
            ];

            $messages = [
                'id.require'           => 'id必须',
                'carousel_id.require'  => 'carousel_id必须',
                'title.require'        => 'title必须',
                'tags.require'         => 'tags必须',
                'cover_image.require'  => 'cover_image必须',
                'type.require'         => 'type必须',
                'file_url.require'     => 'file_url必须',
                'click_action.require' => 'click_action必须',
                'jump_url.require'     => 'jump_url必须',
                'sort.require'         => 'sort必须',
                'is_display.require'   => 'is_display必须',
            ];

            $all = $request->all();
            Validate($rules)->message($messages)->check($all);

            $carousel = Db::name('carousel')->where('id', $all['id'])->find();
            if (!$carousel) {
                return json(['code' => -1, 'msg' => '记录不存在']);
            }

            unset($all['id']);
            $cos                = config('basic.cdn');
            $all['file_url']    = str_replace($cos, '', $all['file_url']);
            $all['jump_url']    = str_replace($cos, '', $all['jump_url']);
            $all['cover_image'] = str_replace($cos, '', $all['cover_image']);
            Db::name('carousel')->where('id', $all['id'])->update($all);
            return json(['code' => 0, 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => '失败：' . $e->getMessage()]);
        }

    }

    public function delete(Request $request): Json
    {
        try {
            $id = $param['id'] ?? 0;

            $res = Db::name('carousel')
                ->where('id', $id)
                ->delete();

            return json(['code' => 0, 'msg' => 'success', 'data' => $res]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => 'error：' . $e->getMessage()]);
        }
    }
}
